package com.xuetang9.fujian.view;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

/**
 * @describe: used to implements the window which screenshot
 * Window used to implement a screenshot
* @author:fujian
 * @date:2021/6/9$
 * @Version: V1.0
 * @Package: com.xuetang9.fujian.view
 */
@SuppressWarnings("all")
public class ScreenFrame extends JFrame {
    private BufferedImage screenImage;
    private BufferedImage darkScreenImage;
    private BufferedImage captureImage;
    private BufferedImage backgroundImage;
    private Point startPoint;
    private Point endPoint;
    private Rectangle captureBounds = new Rectangle();
    private Color captureBoundsColor = new Color(0,174,255);
    private ChatFrame chatFrame;
    public ScreenFrame(){
        init();
        setVisible(true);
    }

    public ScreenFrame(ChatFrame chatFrame){
        this.chatFrame = chatFrame;
        init();
        setVisible(true);
    }


    private void init() {
        setTitle("screenshot");
        setExtendedState(JFrame.MAXIMIZED_BOTH);
//        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setUndecorated(true);
        removeAll();
        creatScreenImage();
        creatDarkScreenImage();
        creatBackgroundImage();
        
        installListeners();
    }

    private void creatScreenImage() {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        try {
            Robot robot = new Robot();
            screenImage = robot.createScreenCapture(new Rectangle(new Point(0,0),screenSize));
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }

    private void creatDarkScreenImage() {
        int width = screenImage.getWidth();
        int height = screenImage.getHeight();
        darkScreenImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
        Graphics g = darkScreenImage.getGraphics();
        g.drawImage(screenImage,0,0,null);
        g.setColor(new Color(0,0,0,180));
        g.fillRect(0,0,width,height);
        g.dispose();
    }

    private void creatBackgroundImage() {
        int width = screenImage.getWidth();
        int height = screenImage.getHeight();
        backgroundImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
        Graphics g = backgroundImage.getGraphics();
        g.drawImage(darkScreenImage,0,0,null);
        g.dispose();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(backgroundImage,0,0,this);
    }

    private void installListeners() {
        ScreenCaptureMouseHandler handler = new ScreenCaptureMouseHandler();
        addMouseListener(handler);
        addMouseMotionListener(handler);
    }

    public void createCaptureBounds(){
        int x,y,w,h;
        x = startPoint.x;
        y = startPoint.y;
        w = endPoint.x - x;
        h = endPoint.y - y;
        captureBounds.setBounds(x,y,w,h);
    }

    private class ScreenCaptureMouseHandler extends MouseAdapter{
        @Override
        public void mousePressed(MouseEvent e) {
            startPoint = e.getPoint();
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            endPoint = e.getPoint();
            createCaptureBounds();
            if (!captureBounds.isEmpty()){
                captureImage = screenImage.getSubimage(captureBounds.x,captureBounds.y,captureBounds.width,captureBounds.height);
                Graphics g = backgroundImage.getGraphics();
                g.drawImage(darkScreenImage,0,0,null);
                g.drawImage(captureImage,captureBounds.x,captureBounds.y,null);
                g.setColor(captureBoundsColor);
                g.drawRect(captureBounds.x,captureBounds.y,captureBounds.width,captureBounds.height);
                g.dispose();
                repaint();
            }
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            chatFrame.setCaptureImage(captureImage);
            ScreenFrame.this.dispose();
        }
    }

    public static void main(String[] args) {
        new ScreenFrame();
    }
}
